package com.tdkj.action.front.member.informationManagement;

import java.sql.Timestamp;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.tdkj.action.BaseAction;
import com.tdkj.model.common.BeanFile;
import com.tdkj.model.information.BeanBaseInfo;
import com.tdkj.model.information.BeanExpert;
import com.tdkj.model.user.BeanUser;
import com.tdkj.service.common.IBeanFileService;
import com.tdkj.service.information.IBeanBaseInfoService;
import com.tdkj.service.information.IBeanExpertService;

public class ExpertAction  extends BaseAction{
	private Long eid;//专家id
	private Long bid;//文献id
	private Long pid;//证件图片id
	private String mn;
	private String mt;
	private String cn;
	private String ct;
	private String title;
	private String resource;
	private String content;
	private String filediscription;
	private String uuid;
	private BeanExpert expert;
	private BeanBaseInfo info;
	private IBeanExpertService expertService;
	private IBeanBaseInfoService baseInfoService;
	private IBeanFileService fileService;
	public Long getEid() {
		return eid;
	}
	public void setEid(Long eid) {
		this.eid = eid;
	}
	public Long getBid() {
		return bid;
	}
	public void setBid(Long bid) {
		this.bid = bid;
	}
	public String getMn() {
		return mn;
	}
	public void setMn(String mn) {
		this.mn = mn;
	}
	public String getFilediscription() {
		return filediscription;
	}
	public void setFilediscription(String filediscription) {
		this.filediscription = filediscription;
	}
	public String getUuid() {
		return uuid;
	}
	public void setUuid(String uuid) {
		this.uuid = uuid;
	}
	public IBeanFileService getFileService() {
		return fileService;
	}
	public Long getPid() {
		return pid;
	}
	public void setPid(Long pid) {
		this.pid = pid;
	}
	@Resource
	public void setFileService(IBeanFileService fileService) {
		this.fileService = fileService;
	}
	public IBeanExpertService getExpertService() {
		return expertService;
	}
	public IBeanBaseInfoService getBaseInfoService() {
		return baseInfoService;
	}
	public String getMt() {
		return mt;
	}
	public void setMt(String mt) {
		this.mt = mt;
	}
	public String getCn() {
		return cn;
	}
	public void setCn(String cn) {
		this.cn = cn;
	}
	public String getCt() {
		return ct;
	}
	public void setCt(String ct) {
		this.ct = ct;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getResource() {
		return resource;
	}
	public void setResource(String resource) {
		this.resource = resource;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public BeanExpert getExpert() {
		return expert;
	}
	public void setExpert(BeanExpert expert) {
		this.expert = expert;
	}
	public BeanBaseInfo getInfo() {
		return info;
	}
	public void setInfo(BeanBaseInfo info) {
		this.info = info;
	}
	@Resource
	public void setExpertService(IBeanExpertService expertService) {
		this.expertService = expertService;
	}
	@Resource
	public void setBaseInfoService(IBeanBaseInfoService baseInfoService) {
		this.baseInfoService = baseInfoService;
	}
	public String findExpert(){
		expert=this.getCurrentUser().getBeanExpert();
		Criteria criteria = this.createCriteria(BeanFile.class)
		.add(Restrictions.eq("expert",expert))
		.addOrder(Order.desc("createtime"));// 
		this.queryByPage(criteria, "expertAction!findExpert.action", 10);
		return "expert_content";
	}
	public String findInfos(){
		ct="2";
		Criteria criteria = this.createCriteria(BeanBaseInfo.class)
		.add(Restrictions.eq("moduletype","11"))
		.add(Restrictions.eq("columntype","2"))
		.addOrder(Order.desc("createtime"));// 对状态进行排序
		this.queryByPage(criteria, "expertAction!findInfos.action", 10);
		return "info_list";
	}
	public String findExpertEditPage(){
		expert=expertService.findById(this.getEid(), BeanExpert.class);
		return "expert_edit";
	}
	public String findExpertAddPage(){
		return "expert_add";
	}
	public String findInfo(){
		info=baseInfoService.findById(this.getBid(), BeanBaseInfo.class);
		return "info_content";
	}
	public String deletePhoto() throws Exception{
		HttpServletRequest request = ServletActionContext.getRequest();
		String path=request.getRealPath("");
		path=path.replaceAll("\\\\", "/");
		fileService.deletePhoto(this.getPid(),path);
		return findExpert();
	}
	public String addPhoto(){
		BeanExpert e=expertService.findById(this.getEid(), BeanExpert.class);
		BeanFile f=fileService.findFileByUUID(this.getUuid());
		if(f!=null) {f.setExpert(e);
		f.setDescription(this.getFilediscription());
		fileService.saveOrUpdate(f, BeanFile.class);}
		return findExpert();
	}
	public String editPhoto(){
		BeanFile f=fileService.findById(this.getPid(), BeanFile.class);
		if(f!=null) {
		f.setDescription(this.getFilediscription());
		fileService.saveOrUpdate(f, BeanFile.class);}
		return findExpert();
	}
	public String findInfoEditPage(){
		info=baseInfoService.findById(this.getBid(), BeanBaseInfo.class);
		mt="11";
		mn="专家栏目";
		ct="2";
		cn="专家文献";
		return "info_edit";
	}
	public String findInfoAddPage(){
		mt="11";
		mn="专家栏目";
		ct="2";
		cn="专家文献";
		return "info_add";
	}
	public String editExpert(){
		expertService.saveOrUpdate(this.getExpert(), BeanExpert.class);
		return findExpert();
	}
	public String addInfo(){
		BeanBaseInfo info=new BeanBaseInfo();
		if(this.getTitle()==null&&this.getTitle().equals("")) {
			this.addFieldError("titleVlidate", "标题不能为空");
		}
		if(this.getTitle()!=null)info.setTitle(this.getTitle());
		if(this.getContent()!=null)info.setContent(this.getContent());
		if(this.getMn()!=null)info.setModulename(this.getMn());
		if(this.getMt()!=null)info.setModuletype(this.getMt());
		if(this.getCn()!=null)info.setColumnname(this.getCn());
		if(this.getCt()!=null)info.setColumntype(this.getCt());
		info.setCreatetime(new Timestamp(System.currentTimeMillis()));
		info.setClicktime(1);
		info.setStatus("未审核");
		BeanUser u=this.getCurrentUser();
		info.setUser(u);
		baseInfoService.save(info);
		return findInfos();
	}
	public String editInfo(){
		BeanBaseInfo info=baseInfoService.findById(this.getBid(), BeanBaseInfo.class);
		if(this.getTitle()==null&&this.getTitle().equals("")) {
			this.addFieldError("titleVlidate", "标题不能为空");
		}
		if(this.getTitle()!=null)info.setTitle(this.getTitle());
		if(this.getContent()!=null)info.setContent(this.getContent());
		if(this.getResource()!=null)info.setResource(this.getResource());
		if(this.getMn()!=null)info.setModulename(this.getMn());
		if(this.getMt()!=null)info.setModuletype(this.getMt());
		if(this.getCn()!=null)info.setColumnname(this.getCn());
		if(this.getCt()!=null)info.setColumntype(this.getCt());
		baseInfoService.saveOrUpdate(info, BeanBaseInfo.class);
		return findInfos();
	}
	public String deleteInfo(){
		baseInfoService.delete(this.getBid(), BeanBaseInfo.class);
		return findInfos();
	}
}
